<?php
/**
 * Created by Engineer CuiLiwu.
 * Project: deal.
 * Date: 2018/6/29-16:33
 * License Hangzhou orce Technology Co., Ltd. Copyright © 2018
 */

namespace App\Http\Controllers\Web;


use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Repositories\Interfaces\ProjectRepository;
use Illuminate\Http\Request;

/**
 * 前台找项目控制器
 */
class ProjectController extends BaseController
{
    /**
     * @var ProjectRepository
     */
    protected $project;

    protected $user_id;

    public function __construct(ProjectRepository $project)
    {
        parent::__construct();
        $this->project = $project;
        $this->user_id = array_get($this->user, 'id');
    }

    public function index(Request $request)
    {
        $this->validate($request, [
            'type' => 'in:page,all'
        ]);
        $type = $request->get('type', 'page');
        if($type == 'page'){
            $address = $this->project->orderBy('created_at', 'desc')->paginate($this->perPage);
            foreach ($address['data'] as $key=>$data){
                if($data['type'] == 'official'){
                    $address['data'][$key]['type'] = '采购公告';
                }
            }
            return $this->success(ErrorConst::SUCCESS_CODE, $address, true);
        }else{
            $address = $this->project->orderBy('created_at', 'desc')->all();
            foreach ($address['data'] as $key=>$data){
                if($data['type'] == 'official'){
                    $address['data'][$key]['type'] = '采购公告';
                }
            }
            return $this->success(ErrorConst::SUCCESS_CODE, $address['data']);
        }
    }

    public function show($id)
    {
        $address = $this->project->find($id);
        $address['data']['lastProject'] = $this->project->indexNewList(1);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $address['data']);
    }

    /**
     * 创建
     * @param Request $request
     * @return \Illuminate\Support\Collection
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'area_code' => 'required|integer',
            'address' => 'required',
            'receiver' => 'required',
            'phone' => 'required|regex:/^1[345789][0-9]{9}$/',
        ]);
        $id = $request->get('id', null);
        $data = $request->all(['province_code', 'city_code', 'area_code', 'address', 'receiver', 'phone']);
        $data['user_id'] = $this->user_id;

        $ret = $this->project->updateOrCreate(['id' => $id], $data);

        return $this->noDataReturn($ret ? true : false);
    }

    /**
     * 更新
     * @param Request $request
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'province_code' => 'required|integer',
            'city_code' => 'required|integer',
            'area_code' => 'required|integer',
            'address' => 'required',
            'receiver' => 'required',
            'phone' => 'required|regex:/^1[345789][0-9]{9}$/',
        ]);
        $data = $request->all(['province_code', 'city_code', 'area_code', 'address', 'receiver', 'phone']);

        $ret = $this->project->update($data, $id);

        return $this->noDataReturn($ret ? true : false);
    }


    public function delete($id)
    {
        $ret = $this->project->delete($id);
        return $this->noDataReturn($ret ? true : false);
    }
}
